<?php
class Image extends AppModel {
	var $name = 'Image';
	var $displayField = 'name';

	var $validate = array(
		'location' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Debe indicar la foto que desea subir al album',
			),
		),
	);
	
	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $belongsTo = array(
		'Categorie' => array(
			'className' => 'Categorie',
			'foreignKey' => 'categorie_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

	var $hasAndBelongsToMany = array(
		'Tag' => array(
			'className' => 'Tag',
			'joinTable' => 'images_tags',
			'foreignKey' => 'image_id',
			'associationForeignKey' => 'tag_id',
			'unique' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		)
	);
	
	// TODO: error messages
	// TODO: think about method scheme
	// TODO: retrieve different pictures to save from root model
	function saveImage($fileData,$id,$thumbSizeMax,$thumbSizeType,$thumbQuality) {
		App::import('Vendor','ccImageResize', array('file' => 'ccImageResize.class.php'));
                $idCategoria = $id;
                //echo "Categoria: " . $idCategoria;
                $idImagen = $fileData['name'];
		$fileData['name'] = $this->getLocationName($fileData['name']);
		$uploadedPath = WWW_ROOT . "img".DS."album".DS."album_".$idCategoria. DS. "original".DS."imagen_" . $idImagen;
	        if (move_uploaded_file($fileData['tmp_name'], $uploadedPath)) {
                        $resizer = new ccImageResize;
			
			if ($resizer->resizeImage($uploadedPath, WWW_ROOT . "img".DS."album".DS."album_".$idCategoria.DS."thumbnails".DS."imagen_" . $idImagen, $thumbSizeMax,$thumbSizeType,$thumbQuality)) {
				// ok, everything is fine
			} else {
				// error with thumbnail resize
                                die("error 1");
				return NULL;		
			}
			
			if ($resizer->resizeImage($uploadedPath, WWW_ROOT . "img".DS."album".DS."album_" . $idCategoria.DS."medium".DS."imagen_". $idImagen, 600,$thumbSizeType,$thumbQuality)) {
				// ok, everything is fine
			} else {
				// error with medium resize
                            die("error 2");
				return NULL;
			}
			
			if ($resizer->resizeImage($uploadedPath, WWW_ROOT . "img".DS."album".DS."album_" . $idCategoria.DS."large".DS."imagen_". $idImagen, 900,$thumbSizeType,$thumbQuality)) {
				// ok, everything is fine
			} else {
				// error with medium resize
                            die("error 3");
				return NULL;
			}	
					
		} else {
			// TODO: Set error message if move_uploaded_file fails
			//die("Could not move uploaded file ++ " . $fileData['name']);
                        die("Recuerde que el tamaño maximo de cada foto es 2MB");
			return NULL;
		}
		return $fileData['name'];
	}
	
	function delImage($filename, $categorie)
	{
		// TODO: Code for condition where unlink fails
		// TODO: Retrieve different pictures from root model
		unlink(WWW_ROOT . "img".DIRECTORY_SEPARATOR."album".DIRECTORY_SEPARATOR."album_".$categorie.DIRECTORY_SEPARATOR."thumbnails".DIRECTORY_SEPARATOR."imagen_" . $filename);
		unlink(WWW_ROOT . "img".DIRECTORY_SEPARATOR."album".DIRECTORY_SEPARATOR."album_".$categorie.DIRECTORY_SEPARATOR."medium".DIRECTORY_SEPARATOR."imagen_" . $filename);
		unlink(WWW_ROOT . "img".DIRECTORY_SEPARATOR."album".DIRECTORY_SEPARATOR."album_".$categorie.DIRECTORY_SEPARATOR."large".DIRECTORY_SEPARATOR."imagen_" . $filename);
		unlink(WWW_ROOT . "img".DIRECTORY_SEPARATOR."album".DIRECTORY_SEPARATOR."album_".$categorie.DIRECTORY_SEPARATOR."original".DIRECTORY_SEPARATOR."imagen_" . $filename);
		return true;
	}
	
	private function getLocationName($fileName) {
		if(file_exists(WWW_ROOT . "img/original/" . $fileName))
		{
			$found = true;
			for($i = 1; $found == true; $i++)
			{
				$proposedName = $i . $fileName;
				$found = file_exists(WWW_ROOT . "img/original/" . $proposedName);
			}
			return $proposedName;
		}
		else
		{
			return $fileName;
		}
	}
	
}
?>